package com.abpa.util.unidata.impl;

import com.abpa.util.unidata.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author msowders
 */
public class CommandBuilderImpl implements CommandBuilder{
    private enum Type { LIST, LISTDICT, WRITE, DELETE }
    private static final String TOXML_ELEMENTS = "TOXML ELEMENTS";
    private static final String SCHEMA_ONLY = "SCHEMAONLY";
    private Command command = new CommandImpl();
    private String type = "";
    private Collection<String> fields = new ArrayList();
    private Collection<String> conditions = new ArrayList();
    private String file = "";
    private String id = null;
    private String sortBy = null;
    private Integer sample = -1;
    private boolean schemaOnly = false;
    private boolean toXmlElements = false;

    public CommandBuilder setId(String id) {
        this.id = id;
        return this;
    }

    public CommandBuilder requireFields(String... fields) {
        for(String field : fields){
            this.fields.add(field);
        }
        return this;
    }

    public CommandBuilder appendCondition(String field, String op, String... values) {
        StringBuilder condition = new StringBuilder();
        condition.append("WITH ");
        condition.append(field);
        condition.append(" ");
        condition.append(op);
        for(String value : values){
            condition.append(" \"");
            condition.append(value);
            condition.append("\"");
        }
        this.conditions.add(condition.toString());
        return this;
    }
    public CommandBuilder schemaOnly() {
        this.schemaOnly = true;
        return this;
    }

    public CommandBuilder toXmlElements() {
        this.toXmlElements = true;
        return this;
    }

    public CommandBuilder sortBy(String field) {
        this.sortBy = field;
        return this;
    }
    public Command build() {
        StringBuilder builder = new StringBuilder();
        builder.append(this.type);
        builder.append(" ");
        builder.append(file);
        if(id!=null){
            builder.append(" \"");
            builder.append(id);
            builder.append("\"");
        }
        for(String condition : this.conditions){
            builder.append(" ");
            builder.append(condition);
        }
        for(String field : this.fields){
            builder.append(" ");
            builder.append(field);
        }
        if(this.sortBy!=null){
            builder.append(" ");
            builder.append("BY ");
            builder.append(this.sortBy);
        }
        if(this.sample>-1){
            builder.append(" ");
            builder.append("SAMPLE ");
            builder.append(sample);
        }
        if(toXmlElements){
            builder.append(" ");
            builder.append(TOXML_ELEMENTS);
        }
        if(schemaOnly){
            builder.append(" ");
            builder.append(SCHEMA_ONLY);
        }
        command.setCommand(builder.toString());
        return command;
    }

    public CommandBuilder list(String file) {
        this.type = "LIST";
        this.file = file;
        this.command.setType(Command.Type.LIST);
        return this;
    }

    public CommandBuilder listdict(String file) {
        this.type = "LIST DICT";
        this.file = file;
        this.command.setType(Command.Type.LISTDICT);
        return this;
    }

    public CommandBuilder sample(Integer max) {
        this.sample = max;
        return this;
    }

}
